package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/admin/shop")
@Api(tags = "店铺营业状态接口")
public class ShopController {

    public static final String SHOP_STATUS = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置店铺营业状态
     * @param status
     * @return
     */
    @ApiOperation(value = "设置店铺营业状态")
    @PutMapping("/{status}")
    public Result<String> updateShopStatus(@PathVariable Integer status) {

        redisTemplate.opsForValue().set(SHOP_STATUS, status);

        return Result.success("修改营业状态成功");
    }

    /**
     * 管理端获取店铺营业状态
     * @return
     */
    @ApiOperation(value = "管理端获取店铺营业状态")
    @GetMapping("/status")
    public Result<Integer> getShopStatus() {
        Integer status = (Integer) redisTemplate.opsForValue().get(SHOP_STATUS);

        if (status == null) {
            redisTemplate.opsForValue().set(SHOP_STATUS, 1);
            // 首次进入页面 手动获取状态
            status = 1;
        }
        return Result.success(status);
    }
}
